<script setup lang="ts">
import moment from "moment";
import { Search, Plus } from "@element-plus/icons-vue";
import SearchItem from "../../components/SearchItem.vue";
import TableCell from "../../components/TableCell.vue";
import EditDrawer from "../../components/EditDrawer.vue";
import LogDialog from "../../components/LogDialog.vue";
import { deleteRoleAPI } from "../../api/role";
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import useTable from "./useTable";
import useForm from "./useForm";
import useMenus from "./useMenus";
import { useRoute } from "vue-router";
import { useRoleStore } from "../../store/role.js";

const path = useRoute().path;
const permissions = useRoleStore().permissions;
const permission = permissions[path] || {};
console.log('permission', permission)

const logVisible = ref(false);
// 查询条件
let filters = reactive({
    name: "",
    pageNo: 1,
    pageSize: 10,
});

// 列表数据
const { tableData, loading, totalCount, createTime, query } = useTable(filters);

// 编辑表单
const { dialogVisible, formRef, isEdit, form, handleEdit, handleConfirm } =
    useForm(query);

// 配置菜单
const {
    menusTree,
    treeRef,
    treeLoading,
    menusVisible,
    handleMenus,
    handleMenusConfirm,
} = useMenus(query);

// 删除
const deleteRole = (id: string) => {
    deleteRoleAPI({
        id,
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "删除成功",
                type: "success",
            });
            query();
        }
    });
};

const rules = reactive({
    name: [
        { required: true, message: "请输入角色名称", trigger: "blur" },
        { min: 1, max: 20, message: "长度不超过20个字", trigger: "blur" },
    ],
});
</script>

<template>
    <div>
        <el-card class="search-wrapper common-card">
            <el-row :gutter="24">
                <SearchItem label="角色名称">
                    <el-input clearable style="width: 100%" placeholder="请输入角色名称" v-model="filters.name"></el-input>
                </SearchItem>
            </el-row>

            <div class="search-button-wrapper">
                <el-button v-if="permission['新增']" :icon="Plus" plain auto-insert-space @click="() => handleEdit(false)">新增</el-button>
                <el-button type="primary" :icon="Search" auto-insert-space @click="query">查询</el-button>
            </div>
        </el-card>

        <!-- Table -->
        <el-card class="common-card" style="width: 100%">
            <!-- 列表 -->
            <el-table border v-loading="loading" :data="tableData" stripe>
                <el-table-column prop="id" label="id" width="120" />
                <el-table-column prop="name" label="角色名称" />
                <el-table-column prop="remark" label="备注" />
                <el-table-column label="创建时间" width="200">
                    <template #default="scope">
                        {{
                        moment(scope.row.create_time).format("YYYY-MM-DD HH:mm:ss")
                        }}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="200">
                    <template #default="scope">
                        <el-button v-if="permission['编辑']" type="text" @click="() => handleEdit(scope.row)">编辑</el-button>
                        <el-button v-if="permission['配置菜单']" type="text" @click="() => handleMenus(scope.row)">配置菜单</el-button>
                        <!-- 删除 -->
                        <el-popconfirm v-if="permission['删除']" :title="`确定删除吗?`" @confirm="deleteRole(scope.row.id)">
                            <template #reference>
                                <el-button class="color_danger" type="text">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>

                <template #empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top: 20px; justify-content: flex-end" background layout="total, sizes, prev, pager, next" :total="totalCount" :default-current-page="1" v-model:current-page="filters.pageNo" v-model:page-size="filters.pageSize"></el-pagination>
            <div v-if="permission['操作日志']" style="text-align: center; margin-top: 10px">
                <el-button type="primary" link @click="logVisible = true">查看操作日志</el-button>
            </div>
        </el-card>

        <EditDrawer :title="isEdit ? '编辑' : '新增'" :visible="dialogVisible" @update:visible="dialogVisible = $event" @confirm="handleConfirm">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="角色名称" prop="name">
                    <el-input style="width: 400px" maxlength="20" v-model="form.name" placeholder="最多可输入20个字"></el-input>
                </el-form-item>
                <el-form-item label="角色备注">
                    <el-input style="width: 400px" maxlength="50" v-model="form.remark" placeholder="最多可输入50个字"></el-input>
                </el-form-item>
            </el-form>
        </EditDrawer>

        <EditDrawer title="配置菜单" :visible="menusVisible" @update:visible="menusVisible = $event" @confirm="handleMenusConfirm">
            <div style="min-height: 500px">
                <el-tree v-loading="treeLoading" ref="treeRef" :data="menusTree" check-on-click-node :expand-on-click-node="false" show-checkbox default-expand-all node-key="id" highlight-current :props="{children: 'children', label: 'label'}"></el-tree>
            </div>
        </EditDrawer>

        <LogDialog :visible="logVisible" :path="path" @update:visible="logVisible = $event"></LogDialog>
    </div>
</template>

<style lang="less" scoped>
@import url("../../assets/less/common.less");
@import url("../../assets/less/search.less");
</style>